package com.ybxx.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 功能：日期辅助类
 * 公司：XA.Co.LTD
 * 制作人：Tom.Y
 * 日期：2011-05*/
public class DateUtil{
	private static final Log logger = LogFactory.getLog(DateUtil.class);

	public static Calendar setStartDay(Calendar cal){
		cal.set(11, 0);
		cal.set(12, 0);
		cal.set(13, 0);
		return cal;
	}

	public static Calendar setEndDay(Calendar cal) {
		cal.set(11, 23);
		cal.set(12, 59);
		cal.set(13, 59);
		return cal;
	}

	public static void copyYearMonthDay(Calendar destCal, Calendar sourceCal){
		destCal.set(1, sourceCal.get(1));
		destCal.set(2, sourceCal.get(2));
		destCal.set(5, sourceCal.get(5));
	}
	public static String formatDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		return sdf.format(date);
	}
	
	public static String formatDateYYYYMMdd(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

		return sdf.format(date);
	}
	public static String formatEnDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

		return sdf.format(date).replaceAll("上午", "AM").replaceAll("下午", "PM");
	}
	public static String formatDateTime(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return sdf.format(date);
	}
	
	public static String formatDateTimeSSS(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S");
		
		return sdf.format(date);
	}
	public static Date parseDate(String dateString) {
		Date date = null;
		try {
			date = DateUtils.parseDate(dateString, new String[] { "yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd","MM/dd/yyyy" });
		} catch (Exception ex) {
			logger.error("Pase the Date(" + dateString + ") occur errors:" + ex.getMessage());
		}
		return date;
	}
	
	public static String addYearFormatDate(Date date,int year,int month,int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.add(Calendar.MONTH, month);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calendar.getTime());
	}
	
	public static boolean sign(String oldtime, String time2,String settime)
	{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		boolean flag = false;
		try
		{
			Date old = df.parse(oldtime);
			Date old2 = df.parse(time2);
			long dtime = old2.getTime() - old.getTime();
			int dt = (int) dtime;
			int stime = Integer.parseInt(settime) * 60 * 60 * 1000;
			int n = Math.abs(dt) - stime;
			if(n>0){
				flag=true;
			}
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return flag;
	}
	public static String addMonthFormDate(String dateStr,int month){
		Date date = new Date();//当前日期  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化对象  
		Calendar calendar = Calendar.getInstance();//日历对象  
		calendar.setTime(java.sql.Date.valueOf(dateStr));//设置当前日期  
		calendar.add(Calendar.MONTH, -1);//月份减一搜索  
		return sdf.format(calendar.getTime());//输出格式化的日期
	}
	public static void main(String[] args){
		String result=addMonthFormDate("2015-01-01",-1);
		System.out.println(result);
	}
}
